//
//  WelcomeViewController.swift
//  SinaWeibo
//
//  Created by apple on 15/11/12.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelcomeViewController: UIViewController {

    override func loadView() {
        
        view = welocmeView
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()

        setUPUI()
    }
    
    //不要在viewdidLoad方法中开始动画
    override func viewWillAppear(animated: Bool) {
        
        super.viewWillAppear(animated)
        startAnimation()
    }
    //MARK 设置动画
    private func startAnimation () {
        //外部改变约束
        self.iconView.snp_updateConstraints { (make) -> Void in
            
            make.bottom.equalTo(self.view.snp_bottom).offset(-self.view.bounds.height + 180)
        }
        
        
        //添加动画效果
        
        UIView.animateWithDuration(1, delay: 0.25, usingSpringWithDamping: 0.9, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
               
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    self.welcomeLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        
                        //发出通知
                        NSNotificationCenter.defaultCenter().postNotificationName(appSwitchToViewController, object: nil)
                })
                
        }
        
        
    }

    //MARK 添加子视图 
    private func setUPUI () {
        
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        //利用SnapKit 设置布局
        //设置头像的约束
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.width.equalTo(90)
            make.height.equalTo(90)
            
        }
        
        //设置label 的约束
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(20)
        }
        //异步下载图片 下载头像
       iconView.sd_setImageWithURL(UserAccountViewModel().headURL, placeholderImage: UIImage(named: "avatar_default_big"))
        
        //修改头像圆角
        iconView.layer.cornerRadius = 45
        iconView.clipsToBounds = true
        
    
    }
    //背景视图
    private lazy var welocmeView :UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //头像
    private lazy var iconView:UIImageView = UIImageView()
    //
    private lazy var welcomeLabel:UILabel = {
        
        let l  = UILabel()
        l.alpha = 0
        l.font = UIFont.systemFontOfSize(18)
        l.text = (UserAccountViewModel().name ?? "") + "欢迎归来"
        l.textAlignment = NSTextAlignment.Center
        l.sizeToFit()
        return l 
        
    }()
    
        
}
